package com.huawei.hms.common;

import android.app.Activity;
import android.content.Context;
import android.os.Looper;
import android.text.TextUtils;
import com.huawei.hianalytics.hms.HiAnalytics;
import com.huawei.hianalytics.hms.HiAnalyticsConf.Builder;
import com.huawei.hms.api.Api;
import com.huawei.hms.api.Api.ApiOptions;
import com.huawei.hms.common.internal.AbstractClientBuilder;
import com.huawei.hms.common.internal.AnyClient;
import com.huawei.hms.common.internal.ClientSettings;
import com.huawei.hms.common.internal.ConnectionManagerKey;
import com.huawei.hms.common.internal.HuaweiApiManager;
import com.huawei.hms.common.internal.HuaweiApiManager.ConnectionManager;
import com.huawei.hms.common.internal.TaskApiCall;
import com.huawei.hms.framework.network.grs.GrsBaseInfo;
import com.huawei.hms.framework.network.grs.GrsClient;
import com.huawei.hms.framework.network.grs.IQueryUrlCallBack;
import com.huawei.hms.framework.network.restclient.dnkeeper.DNKeeperConfig;
import com.huawei.hms.support.api.client.Status;
import com.huawei.hms.support.api.client.SubAppInfo;
import com.huawei.hms.support.api.entity.auth.Scope;
import com.huawei.hms.support.hianalytics.b;
import com.huawei.hms.support.log.HMSLog;
import com.huawei.hms.utils.Checker;
import com.huawei.hms.utils.Util;
import java.lang.ref.WeakReference;
import java.util.Collections;
import java.util.List;
import o.fjo;
import o.fjx;

public class HuaweiApi<TOption extends ApiOptions> {
    private HuaweiApiManager a;
    private Context b;
    private ConnectionManagerKey<TOption> c;
    private TOption d;
    private AbstractClientBuilder<?, TOption> e;
    private String f;
    private String g;
    private String h;
    private SubAppInfo i;
    private WeakReference<Activity> j;
    private int k;

    public HuaweiApi(Activity activity, Api<TOption> api, TOption tOption, AbstractClientBuilder abstractClientBuilder, int i) {
        Checker.checkNonNull(activity, "Null activity is not permitted.");
        this.j = new WeakReference(activity);
        a(activity.getApplicationContext(), api, tOption, abstractClientBuilder, i);
    }

    public HuaweiApi(Activity activity, Api<TOption> api, TOption tOption, AbstractClientBuilder abstractClientBuilder) {
        Checker.checkNonNull(activity, "Null activity is not permitted.");
        this.j = new WeakReference(activity);
        a(activity.getApplicationContext(), api, tOption, abstractClientBuilder, 0);
    }

    public HuaweiApi(Context context, Api<TOption> api, TOption tOption, AbstractClientBuilder abstractClientBuilder, int i) {
        Checker.checkNonNull(context, "Null context is not permitted.");
        a(context.getApplicationContext(), api, tOption, abstractClientBuilder, i);
    }

    public HuaweiApi(Context context, Api<TOption> api, TOption tOption, AbstractClientBuilder abstractClientBuilder) {
        Checker.checkNonNull(context, "Null context is not permitted.");
        a(context.getApplicationContext(), api, tOption, abstractClientBuilder, 0);
    }

    private void a(Context context, Api<TOption> api, TOption tOption, AbstractClientBuilder abstractClientBuilder, int i) {
        this.b = context;
        this.a = HuaweiApiManager.getInstance(this.b);
        this.c = ConnectionManagerKey.createConnectionManagerKey(api, tOption);
        this.d = tOption;
        this.e = abstractClientBuilder;
        this.f = Util.getAppId(context);
        this.g = this.f;
        this.h = Util.getCpId(context);
        this.i = new SubAppInfo("");
        this.k = i;
        a(context);
    }

    private void a(Context context) {
        boolean initFlag = HiAnalytics.getInitFlag();
        HMSLog.i("HuaweiApi", "Builder->biInitFlag :" + initFlag);
        if (!initFlag && Util.isChinaROM()) {
            initFlag = Util.getBiSetting(this.b);
            HMSLog.i("HuaweiApi", "Builder->biSetting :" + initFlag);
            if (!initFlag) {
                GrsBaseInfo grsBaseInfo = new GrsBaseInfo();
                grsBaseInfo.setAppName("opensdkservice");
                grsBaseInfo.setIssueCountry("CN");
                new GrsClient(context, grsBaseInfo).ayncGetGrsUrl("com.huawei.cloud.hianalytics.issue", DNKeeperConfig.SERVICE_KEY, new IQueryUrlCallBack(this) {
                    final /* synthetic */ HuaweiApi a;

                    {
                        this.a = r1;
                    }

                    public void onCallBackSuccess(String str) {
                        if (!TextUtils.isEmpty(str)) {
                            new Builder(this.a.b).setEnableImei(false).setEnableUDID(false).setEnableSN(false).setCollectURL(0, str).setCollectURL(1, str).setAppID("com.huawei.hwid").create();
                            HMSLog.i("HuaweiApi", "BI URL is " + str);
                        }
                    }

                    public void onCallBackFail(int i) {
                        HMSLog.e("HuaweiApi", "get grs failed, the errorcode is " + i);
                    }
                });
            }
        }
    }

    public <TResult, TClient extends AnyClient> fjo<TResult> doWrite(TaskApiCall<TClient, TResult> taskApiCall) {
        if (taskApiCall == null) {
            HMSLog.e("HuaweiApi", "in doWrite:taskApiCall is null");
            fjx o_fjx = new fjx();
            o_fjx.ॱ(new ApiException(Status.FAILURE));
            return o_fjx.ॱ();
        }
        b.a(this.b, taskApiCall.getUri(), TextUtils.isEmpty(this.i.getSubAppID()) ? this.g : this.i.getSubAppID(), taskApiCall.getTransactionId(), String.valueOf(getKitSdkVersion()));
        return a((TaskApiCall) taskApiCall);
    }

    public ConnectionManagerKey<TOption> getConnectionManagerKey() {
        return this.c;
    }

    public fjo<Boolean> disconnectService() {
        fjx o_fjx = new fjx();
        this.a.disconnectService(this, o_fjx);
        return o_fjx.ॱ();
    }

    private <TResult, TClient extends AnyClient> fjo<TResult> a(TaskApiCall<TClient, TResult> taskApiCall) {
        fjx o_fjx = new fjx();
        this.a.sendRequest(this, taskApiCall, o_fjx);
        return o_fjx.ॱ();
    }

    public AnyClient getClient(Looper looper, ConnectionManager connectionManager) {
        return this.e.buildClient(this.b, getClientSetting(), connectionManager, connectionManager);
    }

    protected ClientSettings getClientSetting() {
        ClientSettings clientSettings = new ClientSettings(this.b.getPackageName(), this.b.getClass().getName(), getScopes(), this.f, null, this.i);
        clientSettings.setCpID(this.h);
        if (this.j != null) {
            clientSettings.setCpActivity((Activity) this.j.get());
        }
        return clientSettings;
    }

    protected List<Scope> getScopes() {
        return Collections.emptyList();
    }

    public boolean setSubAppInfo(SubAppInfo subAppInfo) {
        HMSLog.i("HuaweiApi", "Enter setSubAppInfo");
        if (subAppInfo == null) {
            HMSLog.e("HuaweiApi", "subAppInfo is null");
            return false;
        }
        Object subAppID = subAppInfo.getSubAppID();
        if (TextUtils.isEmpty(subAppID)) {
            HMSLog.e("HuaweiApi", "subAppId is empty");
            return false;
        } else if (subAppID.equals(this.f)) {
            HMSLog.e("HuaweiApi", "subAppId is host appid");
            return false;
        } else {
            this.i = new SubAppInfo(subAppInfo);
            return true;
        }
    }

    public String getAppID() {
        return this.g;
    }

    public String getSubAppID() {
        return this.i.getSubAppID();
    }

    public Context getContext() {
        return this.b;
    }

    public TOption getOption() {
        return this.d;
    }

    public int getKitSdkVersion() {
        return this.k;
    }

    public void setKitSdkVersion(int i) {
        this.k = i;
    }
}
